<?php
class exportCurrentAction extends AdminMaster {
	public function execute($request) {
		$action = $request->getParameter ( 'action_name', '' );
		$id = intval ( $request->getParameter ( 'id', 0 ) );
		
		$candidate = CandidatesTable::getDataById ( $id );
		
		if ($action == ADMIN_DEFINED::EXPORT && count ( $candidate ) > 0) {
			$ex = new PHPExcel ();
			$ex->getProperties ()
				->setCreator ( sfConfig::get ( 'app_excel_creator' ) )
				->setLastModifiedBy ( sfConfig::get ( 'app_excel_creator' ) )
				->setTitle ( sfConfig::get ( 'app_excel_candidate-title' ) )
				->setSubject ( sfConfig::get ( 'app_excel_candidate-title' ) )
				->setDescription ( sfConfig::get ( 'app_excel_candidate-title' ) );
			$ex->setActiveSheetIndex ( 0 )
				->setTitle ( sfConfig::get ( 'app_excel_candidate-title' ) );
			$ex->getActiveSheet ()
				->mergeCells ( 'B2:G2' )
				->setCellValue ( 'B2', $candidate ['full_name'] )
				->setCellValue ( 'B3', __ ( 'date_apply', null, 'excel_list' ) )
				->setCellValue ( 'C3', __ ( 'date_interview', null, 'excel_list' ) )
				->setCellValue ( 'D3', __ ( 'company_name', null, 'excel_list' ) )
				->setCellValue ( 'E3', __ ( 'company_address', null, 'excel_list' ) )
				->setCellValue ( 'F3', __ ( 'company_phone_number', null, 'excel_list' ) )
				->setCellValue ( 'G3', __ ( 'result', null, 'excel_list' ) );
				
			$interview = CandidateToJobsTable::getListByCandidateId ( $id );
			$count_i = 3;
			foreach ( $interview as $m ) {
				$count_i ++;
				$ex->getActiveSheet ()
					->setCellValue ( 'B' . $count_i, DateHelper::dateFormatFree ( $m ['date_apply'], 'Y-m-d' ) )
					->setCellValue ( 'C' . $count_i, DateHelper::dateFormatFree ( $m ['date_interview'], 'Y-m-d' ) )
					->setCellValue ( 'D' . $count_i, $m ['Jobs']['Companies']['company_name'] )
					->setCellValue ( 'E' . $count_i, $m ['Jobs'] ['Companies']['address'] )
					->setCellValue ( 'F' . $count_i, $m ['Jobs'] ['Companies']['phone_number'] )
					->setCellValue ( 'G' . $count_i, __ ( 'result_status_' . str_replace ( '-', '_', $m ['result_status'] ), null, 'array_data' ) );
			}
			// APPLY STYLE
			$style = array (
					'borders' => array (
							'allborders' => array (
									'style' => PHPExcel_Style_Border::BORDER_THIN 
							) 
					) 
			);
			$ex->getActiveSheet ()->getStyle ( 'B3:G' . $count_i )->applyFromArray ( $style );
			
			$style = array (
					'font' => array (
							'bold' => true,
							'size' => '13' 
					),
					'alignment' => array (
							'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER 
					) 
			);
			$ex->getActiveSheet ()->getStyle ( 'B2' )->applyFromArray ( $style );
			
			$style = array (
					'fill' => array (
							'type' => PHPExcel_Style_Fill::FILL_SOLID,
							'color' => array (
									'rgb' => 'dedede' 
							) 
					),
					'font' => array (
							'color' => array (
									'rgb' => '0000ff' 
							) 
					) 
			);
			$ex->getActiveSheet ()->getStyle ( 'B3:G3' )->applyFromArray ( $style );
			// ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			// END
			
			$exIO = PHPExcel_IOFactory::createWriter ( $ex, 'Excel2007' );
			ob_end_clean ();
			
			header ( 'Content-Type: application/vnd.ms-excel' );
			header ( 'Content-Disposition: attachment;filename="' . sfConfig::get ( 'app_excel_file-name' ) . '.' . date ( 'Y-m-d', time () ) . '.xlsx"' );
			header ( 'Cache-Control: max-age=0' );
			
			$exIO->save ( 'php://output' );
			die ();
		} else {
			echo __ ( 'hacking_attemp', null, 'table_list' );
			die ();
		}
	}
}